<?php

namespace app\home\admin;
use app\admin\controller\Admin;
use app\home\model\Charge;
use app\home\model\User;
use think\Db;
use think\Loader;
use think\Request;

class Charges extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
//        根据角色来获取对应的用户列表
        $data = $request->param();
//        $validate = validate('Users');
//        $res = $validate->scene('list')->check($data);
//        if (!isset($data['role'])){
//            $data['role'] = 1;
//        }
//        if (!$res){
//            return $this->error($validate->getError());
//        }
//        //设置默认筛选条件
//        $where['ischeck'] = ['in',[0,1]];
        $where['status'] = ['in',[1,2]];
//        $where['role'] = $data['role'];
//
//        //如果设置了删选是否审核
//        if (isset($data['ischeck']) && $data['ischeck'] != 3){
//            if ($data['ischeck'] == 1){
//                $where['ischeck'] = 0;
//            }else{
//                $where['ischeck'] = 1;
//            }
//
//        }
//        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['name'] = ['like',"%{$data['keyword']}%"];
        }
        //设置筛选是否展示
        if (isset($data['status']) && $data['status'] != 3){
            $where['status'] = $data['status'];
        }
//        if (!empty($data['create_time'])){
//            $time = explode('~',$data['create_time']);
//            $start = strtotime($time[0]);
//            $end = strtotime($time[1]);
//            $where['create_time'] = ['between',[$start,$end]];
//        }
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $listrow = $data['listrow'] :  $listrow = 10;
        $res = model('charge')->where($where)->paginate($listrow,true,['page'=>$page]);
        if ($res){
            $this->assign('chargelist',$res);
//            $this->assign('role',$data['role']);
        }else{
            return $this->success('无数据');
        }
        return $this->fetch();


    }

    /**
     * 充值记录
     *
     * @return \think\Response
     */
    public function chrecord(Request $request)
    {
        $data = $request->param();
        //消费状态
        $where['o.status'] = ['in',[1,2]];
        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['u.nickname'] = ['like',"%{$data['keyword']}%"];
        }
        //设置筛选是否展示
        if (isset($data['status']) && $data['status'] != 3){
            $where['o.status'] = $data['status'];
        }

        //设置是否筛选
        if (!empty($data['create_time'])){
            $time = explode('~',$data['create_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $where['o.create_time'] = ['between',[$start,$end]];
        }
        //设置是否筛选
        if (!empty($data['pay_time'])){
            $time = explode('~',$data['pay_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $where['o.pay_time'] = ['between',[$start,$end]];
        }
        $orderlist = \db('home_orders')->alias('o')
            ->join('home_users u','o.uid = u.id')
            ->join('home_charge c','o.chargeid = c.id')
            ->field('o.id,o.ordersn,o.price,o.count,o.status,o.create_time,o.pay_time,o.paytype,u.nickname,c.name')
            ->order('o.create_time desc')
            ->where($where)
            ->paginate(13)
        ->each(function ($item){
             $item['create_time'] = empty($item['create_time']) ? '无' : date('Y-m-d H:i:s',$item['create_time']);
             $item['pay_time'] = empty($item['pay_time']) ? '未成功支付' : date('Y-m-d H:i:s',$item['pay_time']);
             return $item;
        });
        $totalmoney = \db('home_orders')->where('status',2)->field('sum(price) totalprice')->find();
        $ordermoney = \db('home_orders')->field('sum(price) totalprice')->find();
        $alipaymoney = \db('home_orders')->where(['status'=>2,'paytype'=>1])->field('sum(price) totalprice')->find();
        $wechatmoney = \db('home_orders')->where(['status'=>2,'paytype'=>2])->field('sum(price) totalprice')->find();
        empty($totalmoney['totalprice']) ? $totalmoney['totalprice'] = '0.00' : true ;
        empty($ordermoney['totalprice']) ? $ordermoney['totalprice'] = '0.00' : true ;
        empty($alipaymoney['totalprice']) ? $alipaymoney['totalprice'] = '0.00' : true ;
        empty($wechatmoney['totalprice']) ? $wechatmoney['totalprice'] = '0.00' : true ;
        $this->assign('totalmoney',$totalmoney['totalprice']);
        $this->assign('ordermoney',$ordermoney['totalprice']);
        $this->assign('alipaymoney',$alipaymoney['totalprice']);
        $this->assign('wechatmoney',$wechatmoney['totalprice']);
        $this->assign('orderlist',$orderlist);
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delcharge(Request $request)
    {
        $data = $request->param();
        if (isset($data['ids'])){
            $ids = $data['ids'];
            $where['id'] = ['in',$ids];
        }else{
            $where['id'] = $data['id'];
        }

        $res = model('Charge')->where($where)->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function changestatus(Request $request)
    {
        $data = $request->param();
        if (!empty($data['id'])){
            $id = $data['id'];
        }else{
            return $this->error('参数缺失');
        }

        if ($data['val'] == 0){
            $update['status'] = 2;
        }elseif($data['val'] == 1){
            $update['status'] = 1;
        }
        $result = Db::table('rd_home_charge')->where('id',$id)->update($update);
        if ($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}
